<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Traffic Light</title>
  <style>
    .container {
      width: 60px;
      height: 170px;
      border: 5px solid #333;
      overflow: hidden;
      display: flex;
      flex-direction: column;
      align-items: center;
    }
    .light {
      width: 50px;
      height: 50px;
      border-radius: 50%;
      margin: 5px;
      opacity: 0.3;
    }
    .red {
      background-color: red;
    }
    .yellow {
      background-color: yellow;
    }
    .green {
      background-color: green;
    }
    .active {
      opacity: 1;
    }
  </style>
</head>
<body>
  <div class="container">
    <div class="light red"></div>
    <div class="light yellow"></div>
    <div class="light green"></div>
  </div>

  <script>
    const lights = document.getElementsByClassName('light');
    
    let current = 0; // 当前点亮的灯索引

    function changeLight() {
      // 重置所有灯的透明度
      for (let i = 0; i < lights.length; i++) {
        lights[i].classList.remove('active');
      }
      // 点亮当前的灯
      lights[current].classList.add('active');

      // 根据当前的灯决定下一个时间间隔
      let timeout;
      if (current === 0) {
        // 红灯亮3秒
        timeout = 3000;
      } else if (current === 1) {
        // 黄灯亮2秒
        timeout = 2000;
      } else if (current === 2) {
        // 绿灯亮1秒
        timeout = 1000;
      }

      // 切换到下一个灯
      current = (current + 1) % lights.length;

      // 根据设置的时间间隔调用下一次的灯光切换
      setTimeout(changeLight, timeout);
    }

    // 立即调用一次以启动
    changeLight();
  </script>
</body>
</html>
